package gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import client.ClientConsole;

import logic.Librarian;
import logic.Message;
import logic.Reader;

/** 
 * Shows add reader form and delivers user's input.
 * Adds a new reader with the details inserted
 */
public class AddReaderForm extends JPanel implements ActionListener{

	private TextField userName;
	private JPasswordField password;
	private TextField cellphone;
	private TextField email;
	private JButton add;
	private JButton cancel;
	private JButton back;
	private JLabel userNameLabel;
	private JLabel passwordLabel; 
	private JLabel cellphoneLabel; 
	private JLabel emailLabel;
	/** shows feedback from the server */
	private JLabel error;
	/** saves the properties of the GridBagLayout for*/
	GridBagConstraints gbc = new GridBagConstraints();
	
	/**Default constructor- Builds the add reader form*/
	public AddReaderForm() {
		super();
		setLayout(new BorderLayout());
		JPanel panel=new JPanel();
		JPanel backPanel=new JPanel();
		panel.setLayout(new GridBagLayout());
		gbc.insets= new Insets(7,7,7,7);
		error= new JLabel();
		error.setVisible(false);
		userNameLabel = new JLabel("user name:");
		userName=new TextField(20);
		passwordLabel = new JLabel("password:");
		password=new JPasswordField(11);
		cellphoneLabel = new JLabel("cellphone:");
		cellphone=new TextField(20);
		emailLabel = new JLabel("email:");
		email=new TextField(20);
		add=new JButton("Add");
		cancel=	new JButton("Cancel");
		back=	new JButton("Back");
		add.addActionListener(this);
		cancel.addActionListener(this);
		back.addActionListener(this);
		gbc.gridx=0;
		gbc.gridy=0;
		panel.add(userNameLabel,gbc);
		gbc.gridx=1;
		gbc.gridy=0;
		panel.add(userName,gbc);
		gbc.gridx=0;
		gbc.gridy=1;
		panel.add(passwordLabel,gbc);
		gbc.gridx=1;
		gbc.gridy=1;
		panel.add(password,gbc);
		gbc.gridx=0;
		gbc.gridy=2;
		panel.add(cellphoneLabel,gbc);
		gbc.gridx=1;
		gbc.gridy=2;
		panel.add(cellphone,gbc);
		gbc.gridx=0;
		gbc.gridy=3;
		panel.add(emailLabel,gbc);
		gbc.gridx=1;
		gbc.gridy=3;
		panel.add(email,gbc);
		gbc.gridx=0;
		gbc.gridy=4;
		panel.add(add,gbc);
		gbc.gridx=1;
		gbc.gridy=4;
		panel.add(cancel,gbc);
		gbc.gridx=1;
		gbc.gridy=6;
		panel.add(error,gbc);
		add(panel,BorderLayout.CENTER);
		gbc.gridx=0;
		gbc.gridy=6;
		backPanel.add(Box.createHorizontalGlue());
		backPanel.add(back);
		add(backPanel,BorderLayout.SOUTH);
		
	}
	
	/**
	 * Checks if all fields filled and sends a a add reader request to server 
	 */
	public void addReader()	
	{
		if (userName.getText().isEmpty()||password.getText().isEmpty()||cellphone.getText().isEmpty()||email.getText().isEmpty())
		{
			error.setVisible(true);
			error.setText("Please fill all fields");
		}
		else
		{
			error.setVisible(false);
			Reader newReader=new Reader(userName.getText(),password.getText(),cellphone.getText(),email.getText());
			Message msg= new Message("Add Reader",newReader);
			ClientConsole.HandleMessageFromGui(msg);
			msg=(Message)ClientConsole.client.getMessage();
			error.setText(msg.getCommand());
			error.setVisible(true);
		}
	}
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if( ae.getSource() == add )
		{
			addReader();
			
		}
		else if ( ae.getSource() == cancel )
		{
			userName.setText("");
			cellphone.setText("");
			email.setText("");
			password.setText("");
			
		}
		else if ( ae.getSource() == back )
		{
			this.setVisible(false);
			if (ClientConsole.reader instanceof LibraryManager)
				ClientConsole.aFrame.setContentPane(new LibraryManagerWindow());
			else if (ClientConsole.reader instanceof Librarian)
				ClientConsole.aFrame.setContentPane(new LibrarianWindow());
			
		}
			

		
	}
	
	
}
